package com.cheng.converter;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//转换，继承转换类
public class BooleanAndInt extends BaseTypeHandler<Boolean> {


    //java(boolean)-DB(number)
    /*
     * ps:PreparedStatement对象
     * i：PreparedStatement对象操作参数的位置
     * parameter:java值
     * jdbcType：jdbc操作的数据库类型
     */

    //插入时
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Boolean aBoolean, JdbcType jdbcType) throws SQLException {
        //java的boolea出入时将数据转为db的int型
        if (aBoolean){
            preparedStatement.setInt(i,1);
        }else
            preparedStatement.setInt(i,0);
    }


    //查出时
    @Override
    public Boolean getNullableResult(ResultSet resultSet, String s) throws SQLException {
        int sexNum = resultSet.getInt(s);
        return sexNum==1?true:false;
    }

    @Override
    public Boolean getNullableResult(ResultSet resultSet, int i) throws SQLException {
        int sexNum = resultSet.getInt(i);
        return sexNum==1?true:false;
    }

    @Override
    public Boolean getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        int sexNum = callableStatement.getInt(i);
        return sexNum==1?true:false;
    }
}
